<?php
/**
 * 
 *
 * PHP version 5.3.x
 *
 * @category 
 * @package  
 * @author   gouki <gouki.xiao@gmail.com>
 */

/**
 * SaeDbConnection.php
 *
 * @category 
 * @package  
 * @author   gouki <gouki.xiao@gmail.com>
 * @version $Id$ 
 * @created 2011-11-22 18:16
 */ 
class SaeDbConnection extends CDbConnection{

    public $isSlave = false;

    const DBUSER = SAE_MYSQL_USER;
    const DBPASS = SAE_MYSQL_PASS;
    const DBHOST_M = SAE_MYSQL_HOST_M;
    const DBHOST_S = SAE_MYSQL_HOST_S;
    const DBPORT = SAE_MYSQL_PORT;
    const DBNAME = SAE_MYSQL_DB;

    public function init(){
        $this->username = self::DBUSER;
        $this->password = self::DBPASS;
        $this->getDbConenction();
        parent::init();
    }

    public function getDbConenction(){
        if($this->isSlave === true){
            $this->connectionString = sprintf("mysql:host=%s;port=%s;dbname=%s",self::DBHOST_S,self::DBPORT,self::DBNAME);
        }else{
            $this->connectionString = sprintf("mysql:host=%s;port=%s;dbname=%s",self::DBHOST_M,self::DBPORT,self::DBNAME);
        }
        return $this->connectionString;
    }


    /**
     *    判断当前的Sql是否select操作
     * @param $query
     * @return int
     */
    protected function isSelectQuery($query){
        return preg_match('/^\s*(SELECT|SHOW|DESCRIBE|PRAGMA)/i',$query);
    }
}
